package com.gsc.controller;

import com.gsc.domain.ResponseResult;
import com.gsc.domain.dto.RoleDto;
import com.gsc.domain.entity.User;
import com.gsc.domain.vo.AddRoleVo;
import com.gsc.domain.dto.ChangeStatusDto;
import com.gsc.domain.dto.RoleListDto;
import com.gsc.domain.entity.Role;
import com.gsc.domain.vo.PageVo;
import com.gsc.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author gscstart
 * @create 2023/2/22-10:33
 */
@RestController
@RequestMapping("/system/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @GetMapping("/list")
    public ResponseResult selectRoleList(Integer pageNum, Integer pageSize, RoleListDto roleListDto){
        PageVo pageVos = roleService.selectRoleList(pageNum, pageSize, roleListDto);
        return ResponseResult.okResult(pageVos);
    }

    /**
     * @param changeStatusDto
     * @return
     */
    @PutMapping("/changeStatus")
    public ResponseResult updateRoleList(@RequestBody ChangeStatusDto changeStatusDto){
        Role role = new Role();
        role.setId(changeStatusDto.getRoleId());
        role.setStatus(changeStatusDto.getStatus());
        return ResponseResult.okResult(roleService.updateById(role));
    }

    /**
     * @param addRoleVo
     * @return
     */
    @PostMapping
    public ResponseResult addRole(@RequestBody AddRoleVo addRoleVo){
        return roleService.addRole(addRoleVo);
    }

    @GetMapping("/{id}")
    public ResponseResult selectRoleById(@PathVariable("id") Long id){
        Role role = roleService.getById(id);
        return ResponseResult.okResult(role);
    }

    @PutMapping
    public ResponseResult updateRole(@RequestBody RoleDto roleDto){
        return roleService.updateRole(roleDto);
    }

    @DeleteMapping("/{id}")
    public ResponseResult deleteRole(@PathVariable("id") Long id){
        roleService.removeById(id);
        return ResponseResult.okResult();
    }

    @GetMapping("/listAllRole")
    public ResponseResult listAllRole(){
        List<Role> roles = roleService.listAllRole();
        return ResponseResult.okResult(roles);
    }

}
